# coding: utf8
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin
import datetime
from wages.models import Staff, Salary, Absent

# Register your models here.

admin.AdminSite.site_header = u'慧信通工资查询'

#
# all_models = [getattr(m, i) for i in m.__dict__ if
#               isinstance(getattr(m, i), m.models.Model.__class__) and getattr(m, i).__module__ == 'mozi.models']


# admin.site.register(all_models)


class StaffResources(resources.ModelResource):
    class Meta:
        model = Staff
        fields = ('staff_name', 'bank_id', 'salary_end')
        # """首次导入"""
        # fields = ('id', 'staff_name', 'department', 'bank_id', 'salary_end', 'salary_base', 'salary_meal',
        #           'salary_traffic', 'retire_insurance', 'unemployment_insurance', 'medical_insurance',
        #           'housing_fund', 'tax')
        export_order = ('staff_name', 'bank_id', 'salary_end')


class SalaryResources(resources.ModelResource):
    class Meta:
        model = Salary
        fields = ('id', 'staff', 'staff__staff_name', 'salary_date', 'salary_month', 'salary_end', 'salary_base',
                  'salary_meal', 'salary_traffic', 'retire_insurance', 'unemploy_insurance',
                  'medical_insurance', 'housing_fund', 'tax')
        export_order = ('id', 'staff', 'staff__staff_name', 'salary_date', 'salary_month', 'salary_end', 'salary_base',
                        'salary_meal', 'salary_traffic', 'retire_insurance', 'unemploy_insurance',
                        'medical_insurance', 'housing_fund', 'tax')


class SalaryInline(admin.TabularInline):
    model = Salary
    fields = ('salary_date', 'salary_month', 'salary_end', 'salary_base',
              'salary_meal', 'salary_traffic', 'retire_insurance', 'unemploy_insurance',
              'medical_insurance', 'housing_fund', 'tax')
    extra = 0
    classes = ['collapse']


class AbsentInline(admin.TabularInline):
    model = Absent
    fields = ('staff', 'absent_date', 'absent_num')
    extra = 0
    classes = ['collapse']

@admin.register(Staff)
class StaffAdmin(ImportExportModelAdmin):
    resource_class = StaffResources
    list_display = ('staff_name', 'department', 'bank_id', 'salary_end', 'salary_base', 'salary_takeoff')
    search_fields = ('staff_name', )
    """TODO: inlines collapse"""
    inlines = [SalaryInline, AbsentInline]
    actions = ['cal_salary', 'create_salary']

    def cal_salary(self, request, queryset):
        for object in queryset:
            object.save()
    cal_salary.short_description = u'计算上月工资'

    def create_salary(self, request, queryset):
        date = datetime.date.today()
        if date.month != 1:
            year = date.year
            month = date.month - 1
        else:
            year =date.year - 1
            month = 12
        for obj in queryset:
            Salary.objects.update_or_create(staff_id=obj.id, salary_end=obj.salary_end, salary_base=obj.salary_base,
                                            salary_date=datetime.date.today(), salary_month=u'%s年%s月' %(year, month),
                                            salary_meal=obj.salary_meal, salary_traffic=obj.salary_traffic,
                                            retire_insurance=obj.retire_insurance,
                                            unemploy_insurance=obj.unemploy_insurance,
                                            medical_insurance=obj.medical_insurance, housing_fund=obj.housing_fund,
                                            salary_takeoff=obj.salary_takeoff, tax=obj.tax)
    create_salary.short_description = u'生成上月工资记录'

@admin.register(Salary)
class SalaryAdmin(ImportExportModelAdmin):
    resource_class = SalaryResources
    list_display = ('staff', 'salary_date', 'salary_end', 'salary_month', 'salary_base')
    search_fields = ('staff__staff_name', )
    list_filter = ('staff', )
    date_hierarchy = 'salary_date'


@admin.register(Absent)
class AbsentAdmin(admin.ModelAdmin):
    list_display = ('staff', 'absent_date', 'absent_num')
    search_fields = ('staff__staff_name', )
    list_filter = ('staff', )